﻿import com.mixmedia.motion.MotionTween;
import com.mixmedia.mx.Language;
/**
   @class  
   @package
   @author Colin Leung
   @codehint 
   @example 
   @tooltip 
*/


class com.mixmedia.navigation.ContentHolder extends MovieClip{
	private var langObj:Language;

	private var loadFileName;
	private var loadFileFullName;
	private var mcImageLoader:MovieClip;
	private var mcFileContainer:MovieClip;
	private var imageloaderByte:Number;
	
	private var fadein_mt:MotionTween;
	private var fadeout_mt:MotionTween;
	
	public var oPreloaderSetting;
	
	private var delegateOnSectionChange:Object;
	private var delegateOnLoadClip:Object;
	private var delegateOnLangChange:Object;
	
	public function ContentHolder(){
		if(typeof(this)!='object'){
			delegateOnSectionChange = mx.utils.Delegate.create(this,onSectionChange);
			delegateOnLoadClip = mx.utils.Delegate.create(this,loadClip);
			delegateOnLangChange = mx.utils.Delegate.create(this,onLangChange);
			
			langObj = Language.instance();
			langObj.addEventListener('onChange',delegateOnLangChange);

			if(mcImageLoader==undefined)mcImageLoader = this.createEmptyMovieClip("mcImageLoader",10);
			fadeout_mt = new MotionTween(mcImageLoader,{a:0,dur:5,forceRun:true});
			fadein_mt = new MotionTween(mcImageLoader,{a:100,dur:10});
			fadeout_mt.addEventListener('onTweenEnd',delegateOnLoadClip);

		}
	}

	private function onLoad(){
		_global.mainNav.addEventListener('onSectionChange',delegateOnSectionChange);
	}

	private function onSectionChange(evt):Void{
		if(evt.loader == this._name || evt.loader == undefined || evt.loader === this){
			swapSection(evt.value);
		}
	}

	private function swapSection(arg){
			loadFileName = arg;
			loadFileFullName = arg+'.swf';
			swapSectionEffect();	
	}

	private function swapSectionEffect(){//can be override, default effect
		fadeout_mt.startTween();
	} 

	private function loadCompleteEffect(){//can be override, default effect
		fadein_mt.startTween();
	}
	
	public function getProgress():Object{
		var s:Object = {bytesLoaded:mcImageLoader.getBytesLoaded(),bytesTotal:mcImageLoader.getBytesTotal()};
		return s;
	};

	private function loadClip(){
		//preloader just use for display loading status.
		var path = (langObj.getLangID()=='')?loadFileFullName:(langObj.getLangID()+'/'+loadFileFullName);
		mcImageLoader.loadMovie(path);
		onEnterFrame = fileOnLoadComplete;
		dispatchEvent({target:this,type:'onLoadInit',value:this,setting:oPreloaderSetting});
	}

	private function fileOnLoadComplete(){
		if(mcImageLoader.getBytesLoaded()/mcImageLoader.getBytesTotal()==1){
			loadCompleteEffect();
			dispatchEvent({target:this,type:'onLoadComplete',value:this});
			delete onEnterFrame;
		}
	}

	private function onLangChange(){
		swapSection(loadFileName);
	}
	
	public function addEventListener(event:String, handler):Void{}
	private function dispatchEvent(eventObj:Object):Void{}
	public function removeEventListener(event:String, handler):Void{}
}